<template>
  <div class="login-container"
       @keyup.enter.native="handleLogin">
    <top-color v-show="false"></top-color>
    <div class="login-weaper animated bounceInDown">
      <div class="login-border" style="width: 100%;">
        <div class="login-main">
          <h4 class="login-title">
            	设置新密码
          </h4>
          <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="login-form">
  <el-form-item label="输入新密码" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="确认新密码" prop="checkPass">
    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="图形验证码" prop="code">
      <el-row :span="24">
        <el-col :span="16">
          <el-input size="small"
                    @keyup.enter.native="handleLogin"
                    v-model="ruleForm.code"
                    auto-complete="off">
            <i slot="prefix" class="icon-yanzhengma"/>
          </el-input>
        </el-col>
        <el-col :span="8">
          <div class="login-code">
            <img :src="ruleForm.image" class="login-code-img" @click="refreshCode"
            />
          </div>
        </el-col>
      </el-row>
    </el-form-item>
  <el-form-item label="手机验证码" prop="age">
  	<el-row :span="24">
        <el-col :span="16">
          <el-input v-model="ruleForm.age"></el-input>
        </el-col>
        <el-col :span="8">
          <div class="login-code">
          	<el-button type="info" plain :disabled="disabledBtn" @click="sendPhoneCode">{{btnMsg}}</el-button>
          </div>
        </el-col>
      </el-row>
  </el-form-item>
  <el-form-item style="padding-top: 10px;">
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
          
        </div>

      </div>
    </div>
  </div>
</template>
<script>
import userLogin from "./userlogin";
import { mapGetters } from "vuex";
import {getCaptcha} from "@/api/user";
import { dateFormat } from "@/util/date";
import { validatenull } from "@/util/validate";
import topLang from "@/page/index/top/top-lang";
import topColor from "@/page/index/top/top-color";
import {getQueryString, getTopUrl} from "@/util/util";
export default {
  name: "login",
  components: {
    userLogin,
    topLang,
    topColor
  },
  data() {
  	var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('年龄不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value < 18) {
              callback(new Error('必须年满18岁'));
            } else {
              callback();
            }
          }
        }, 1000);
      };
      var checkCode = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入图形验证码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
    return {
      time: "",
      btnMsg: "发送短信验证码",
      disabledBtn: false,
      ruleForm: {
          pass: '',
          checkPass: '',
          age: '',
          code: '',
          key:'',
          //预加载白色背景
          image: "",
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          code: [
            { validator: checkCode, trigger: 'blur' }
          ],
          age: [
            { validator: checkAge, trigger: 'blur' }
          ]
        }
    };
  },
  watch: {
    
  },
  created() {
    this.refreshCode();
  },
  mounted() {
  },
  computed: {
    ...mapGetters(["website", "tagWel"])
  },
  props: [],
  methods: {
  	timeoutfn() {
  		let countdown = 60;
  		const self = this;
  		const temOutFn = setInterval(function() {
  			if(countdown == 0) {
  				self.btnMsg = "发送短信验证码";
  				self.disabledBtn = false;
  				clearTimeout(temOutFn);
          return;
  			}else{
  				self.btnMsg = "("+countdown+"秒)后重新发送";
  				countdown--;
  			}
  		},1000)
  	},
  	sendPhoneCode() {
      this.disabledBtn = true;
      //请求校验图形验证码，成功发短信倒计时
      this.timeoutfn();
  		
  	},
  	refreshCode() {
        getCaptcha().then(res => {
          const data = res.data.data;
          this.ruleForm.key = data.key;
          this.ruleForm.image = data.image;
        })
      },
    	submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
  }
};
</script>

<style lang="scss">
@import "@/styles/login.scss";
</style>
